package cn.byapps.chensheng.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;

import cn.byapps.chensheng.service.IndexService;
import cn.byapps.chensheng.vo.WxAccoutVO;
import cn.byapps.chensheng.vo.WxNewVO;

@Controller
public class IndexController {

    @Autowired
    private IndexService indexService;
    @GetMapping({"/","","index","index.html"})
    public String index(Model model,Integer page ,Integer rows){
        if(page == null){
            page = 1;
        }
        if(rows == null){
            rows = 10;
        }
        List<WxAccoutVO> wxAccouts = indexService.findByPage(page,rows);
        model.addAttribute("page", page);
        model.addAttribute("rows", rows);
        model.addAttribute("wxAccouts",wxAccouts);
        return "index";
    }

    @GetMapping("/{appid}/search")
    public String list(@PathVariable String appid,Integer page,Model model){
        Map<String, Object> pageResult = new HashMap<String, Object>();
        
        int rows = 10;
		if (page == null)
             page = 1;
        
        int total = indexService.countAppId(appid);
        int totalPage = (total + rows - 1) / rows;
        if(page > totalPage){
            page = totalPage;
        }
        
        List<WxNewVO> list = indexService.findPageByAppId(appid,page,rows);
        pageResult.put("list", list);
        pageResult.put("currPage", page);
		pageResult.put("totalPage", totalPage);
        model.addAttribute("pageResult", pageResult);
        WxAccoutVO wxAccoutVO = indexService.getAccountByAppId(appid);
        model.addAttribute("wx", wxAccoutVO);
        return "search";
    }
}